--版本控制：1.2.0
local AddonName, Addon = ...

local L = Addon.L

local Config = Addon.Config
local Panel = Addon.Panel
local ScrollFrame = Addon.ScrollFrame

--设置面板初始化
Panel:SetSize(500, 1000)
ScrollFrame.ScrollBar:ClearAllPoints()
ScrollFrame.ScrollBar:SetPoint("TOPLEFT", ScrollFrame, "TOPRIGHT", -20, -20)
ScrollFrame.ScrollBar:SetPoint("BOTTOMLEFT", ScrollFrame, "BOTTOMRIGHT", -20, 20)
ScrollFrame:SetScrollChild(Panel)
ScrollFrame.name = AddonName
InterfaceOptions_AddCategory(ScrollFrame)
--标题
local PanelTitle = Panel:CreateFontString(nil, "ARTWORK", "GameFontNormalLargeLeft")
PanelTitle:SetPoint("TOPLEFT", 16, -16)
PanelTitle:SetText("|cFFFF6633"..AddonName.."|r".." v"..Addon.Version)
--所有组件表
Panel.Controls = {}
--创建唯一命名函数
local UniqueName
do
	local ControlID = 1

	function UniqueName(Name)
		ControlID = ControlID + 1
		return string.format("%s_%s_%02d", AddonName, Name, ControlID)
	end
end
--复制声音列表
function CopyCharacter()
	local List = {}
	local i = 1
	for k, v in pairs(Addon.CharacterList) do
		List[i*2-1] = v
		List[i*2] = k
		i = i + 1
	end
	return List
end

--设置面板确定函数
function ScrollFrame:ConfigOkay()
	for _, control in pairs(Panel.Controls) do
		control:SaveValue(control.CurrentValue)
	end
end
--设置面板回到默认设置函数
function ScrollFrame:ConfigDefault()
	for _, control in pairs(Panel.Controls) do
		control.CurrentValue = control.DefaultValue
		control:SaveValue(control.CurrentValue)
	end
end
--设置面板刷新函数
function ScrollFrame:ConfigRefresh()
	for _, control in pairs(Panel.Controls) do
		control.CurrentValue = control:LoadValue(control)
		control:UpdateValue(control)
	end
end
--创建标题函数
function Panel:CreateHeading(text)
	local Title = self:CreateFontString(nil, "ARTWORK", "GameFontNormalLeft")
	Title:SetText(text)
	return Title
end
--创建文本函数
function Panel:CreateText(text)
	local TextBlob = self:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmallLeft")
	TextBlob:SetText(text)
	return TextBlob
end
--创建按钮函数
function Panel:CreateButton(text, RunFunction)
	local Button = CreateFrame("Button", nil, self, "UIPanelButtonTemplate")
	Button:Enable()
	Button:SetSize(55, 25)
	Button:SetText(text)
	Button:SetScript("OnClick", RunFunction)
	return Button
end
--创建选择框函数
function Panel:CreateCheckBox(text, LoadValue, SaveValue, DefaultValue)
	local CheckBox = CreateFrame("CheckButton", UniqueName("CheckButton"), self, "InterfaceOptionsCheckButtonTemplate")

	CheckBox.LoadValue = function(self)
		self.CurrentValue = LoadValue(self)
		return self.CurrentValue
	end
	CheckBox.SaveValue = SaveValue
	CheckBox.DefaultValue = DefaultValue
	CheckBox.UpdateValue = function(self) self:SetChecked(self.CurrentValue) end
	getglobal(CheckBox:GetName() .. "Text"):SetText(text)
	CheckBox:SetScript("OnClick", function(self) self.CurrentValue = self:GetChecked() end)

	self.Controls[CheckBox:GetName()] = CheckBox
	return CheckBox
end
--下拉菜单点击函数
local function DropDownOnClick(_, DropDown, SelectedValue)
	DropDown.CurrentValue = SelectedValue
	UIDropDownMenu_SetText(DropDown, DropDown.ValueTexts[SelectedValue])
end
--下拉菜单初始化函数
local function DropDownInitialize(frame)
	local info = UIDropDownMenu_CreateInfo()

	for i=1,#frame.ValueList,2 do
		local k, v = frame.ValueList[i], frame.ValueList[i + 1]
		info.text = v
		info.value = k
		info.checked = frame.CurrentValue == k
		info.func = DropDownOnClick
		info.arg1, info.arg2 = frame, k
		UIDropDownMenu_AddButton(info)
	end
end
--创建下拉菜单函数
function Panel:CreateDropDown(text, ValueList, LoadValue, SaveValue, DefaultValue)
	local DropDown = CreateFrame("Frame", UniqueName("DropDown"), self, "UIDropDownMenuTemplate")

	local Title = self:CreateFontString(nil, "ARTWORK", "GameFontNormalSmallLeft")
	Title:SetText(text)
	Title:SetPoint("BOTTOMLEFT", DropDown, "left", -80, -6)

	DropDown.LoadValue = function(self)
		self.CurrentValue = LoadValue(self)
		return self.CurrentValue
	end
	DropDown.SaveValue = SaveValue
	DropDown.DefaultValue = DefaultValue
	DropDown.UpdateValue = function(self)
		UIDropDownMenu_SetText(self, self.ValueTexts[self.CurrentValue])
	end

	DropDown.ValueList = ValueList
	DropDown.ValueTexts = {}
	for i=1,#ValueList,2 do
		local k, v = ValueList[i], ValueList[i + 1]
		DropDown.ValueTexts[k] = v
	end

	DropDown:SetScript("OnShow", function(self)
		UIDropDownMenu_Initialize(self, DropDownInitialize)
	end)

	UIDropDownMenu_JustifyText(DropDown, "LEFT")
	UIDropDownMenu_SetWidth(DropDown, 180)
	UIDropDownMenu_SetButtonWidth(DropDown, 144)

	self.Controls[DropDown:GetName()] = DropDown
	return DropDown
end
--创建输入框函数
function Panel:CreateEditBox(_, LoadValue, SaveValue, DefaultValue)
	local EditBox = CreateFrame("EditBox", UniqueName("EditBox"), self, "InputBoxTemplate")
	if EditBox:IsAutoFocus() then
		EditBox:SetAutoFocus(false)
	end

	EditBox.LoadValue = function(self)
		self.CurrentValue = LoadValue(self)
		return self.CurrentValue
	end
	EditBox.SaveValue = SaveValue
	EditBox.DefaultValue = DefaultValue
	EditBox.UpdateValue = function(self) self:SetText(self.CurrentValue) end
	EditBox:SetScript("OnEditFocusGained", function(self)
		self:HighlightText()
	end)
	EditBox:SetScript("OnEditFocusLost", function(self)
		self:SetText(self.CurrentValue)
	end)
	EditBox:SetScript("OnEnterPressed", function(self)
		if self:HasFocus() then
			self.CurrentValue = self:GetText()
			self:ClearFocus()
		end
	end)
	EditBox:SetScript("OnEscapePressed", function(self)
		if self:HasFocus() then
			self:SetText("")
			self.CurrentValue = self:GetText()
			self:ClearFocus()
		end
	end)
	EditBox:SetScript("OnEditFocusLost", function(self)
		self.CurrentValue = self:GetText()
	end)

	self.Controls[EditBox:GetName()] = EditBox
	return EditBox
end


--设置面板初始化函数
function Panel:Initialize()
	local Author = self:CreateText(L["SOUNDTAQ_AUTHOR"])
	Author:SetPoint("LEFT", PanelTitle, "RIGHT", 180, 0)

	--反馈和更新链接
	local FeedbackHeading = self:CreateText(L["SOUNDTAQ_LINK_TITLE"])
	FeedbackHeading:SetPoint("TOPLEFT", PanelTitle, "BOTTOMLEFT", 30, -30)
	local FeedbackLink = self:CreateEditBox(nil,
		function (self) return L["SOUNDTAQ_LINK"] end,
		function (text) self.CurrentValue = L["SOUNDTAQ_LINK"] end,
		L["SOUNDTAQ_LINK"])
	FeedbackLink:SetMultiLine(true)
	FeedbackLink:SetHeight(30)
	FeedbackLink:SetWidth(355)
	FeedbackLink:SetPoint("LEFT", FeedbackHeading, "RIGHT", 20, 0)

	--启用插件选择框
	local AddonSwitch = self:CreateCheckBox(
		L["SOUNDTAQ_ENABLE"],
		function(self) return Config.IsEnable end,
		function(self, v)	Config.IsEnable = v	end,
		true)
	AddonSwitch:SetPoint("TOPLEFT", PanelTitle, "BOTTOMLEFT", 0, -85)

	--通告语音列表
	local SoundList = self:CreateDropDown(
		L["SOUNDTAQ_SOUNDLIST"],
		CopyCharacter(),
		function(self) return Config.SelectSound end,
		function(self, v) Config.SelectSound = v end,
		"xiaotong")
	SoundList:SetPoint("TOPLEFT", AddonSwitch, "BOTTOMLEFT", 110, -30)

	--输出频道列表
	local OutputChannelList = self:CreateDropDown(
		L["SOUNDTAQ_CHANNELLIST"], 
		{"Master", L["CHANNEL_MASTER"], "SFX", L["CHANNEL_SFX"], "Music", L["CHANNEL_MUSIC"], "Ambience", L["CHANNEL_AMBIENCE"], "Dialog", L["CHANNEL_DIALOG"]},
		function(self) return Config.OutputChannel end,
		function(self, v) Config.OutputChannel = v end,
		"Master")
	OutputChannelList:SetPoint("TOPLEFT", SoundList, "BOTTOMLEFT", 0, -25)

	--试听按钮
	--试听文件下拉菜单
	local FileList = self:CreateDropDown(
		L["SOUNDTAQ_FILELIST"],
		{
			"\\Skeram_Interrupt.mp3", L["SOUND_BOSS1_1"],
			"\\Skeram_Control.mp3", L["SOUND_BOSS1_2"],
			"\\Skeram_WrongTarget.mp3", L["SOUND_BOSS1_3"],
			"\\Viscidus_Prepare.mp3", L["SOUND_BOSS5_1"],
			"\\Viscidus_AOE.mp3", L["SOUND_BOSS5_2"],
			"\\Huhuran_Berserk.mp3", L["SOUND_BOSS6_1"],
			"\\Huhuran_Tranquilizing.mp3", L["SOUND_BOSS6_2"],
			"\\Empires_Teleport.mp3", L["SOUND_BOSS7_1"],
			"\\Empires_Threat.mp3", L["SOUND_BOSS7_2"],
			"\\Ouro_Knockback.mp3", L["SOUND_BOSS8_1"],
			"\\CThun_DarkGlare.mp3", L["SOUND_BOSS9_1"],
			"\\CThun_EyeTentacle.mp3", L["SOUND_BOSS9_2"],
			"\\CThun_10Yards.mp3", L["SOUND_BOSS9_3"],
			"\\CThun_Interrupt.mp3", L["SOUND_BOSS9_4"],
			"\\CThun_GetOut.mp3", L["SOUND_BOSS9_5"],
			"\\Five_Plague.mp3", L["SOUND_5DOG_1"],
		},
		function(self) return Config.TestFile end,
		function(self, v) Config.TestFile = v end,
		"\\Skeram_Interrupt.mp3")
	FileList:SetPoint("TOPLEFT", OutputChannelList, "BOTTOMLEFT", 0, -25)
	--试听按钮
	local TestSound = self:CreateButton(
		L["SOUNDTAQ_TESTSOUND"],
		function(self) ScrollFrame:ConfigOkay() PlaySoundFile(L["SOUND_PATH"]..Config.SelectSound..Config.TestFile) end)
	TestSound:SetPoint("LEFT", FileList, "RIGHT", 2, 3)
	--新增/修改/删除语音包模块
	local EditModeText = self:CreateHeading(L["SOUNDTAQ_EDITMODE"])
	EditModeText:SetPoint("TOPLEFT", AddonSwitch, "BOTTOMLEFT", 10, -200)
	local EditModeCharacterName = self:CreateText(L["SOUNDTAQ_CHARACTER"])
	local EditModeFolderName = self:CreateText(L["SOUNDTAQ_FOLDER"])
	EditModeCharacterName:SetPoint("TOPLEFT", EditModeText, "BOTTOMLEFT", 30, -20)
	EditModeFolderName:SetPoint("LEFT", EditModeCharacterName, "RIGHT", 150, 0)
	local CharacterInputBox = self:CreateEditBox(nil,
		function(self) return "" end,
		function(self, v) self.CurrentValue = v end,
		"")
	CharacterInputBox:SetMultiLine(false)
	CharacterInputBox:SetHeight(30)
	CharacterInputBox:SetWidth(155)
	CharacterInputBox:SetPoint("TOPLEFT", EditModeCharacterName, "BOTTOMLEFT", 5, -15)
	local FolderInputBox = self:CreateEditBox(nil,
		function(self) return "" end,
		function(self, v) self.CurrentValue = v end,
		"")
	FolderInputBox:SetMultiLine(false)
	FolderInputBox:SetHeight(30)
	FolderInputBox:SetWidth(155)
	FolderInputBox:SetPoint("TOPLEFT", EditModeFolderName, "BOTTOMLEFT", 5, -15)
	local AddSound = self:CreateButton(
		L["SOUNDTAQ_OK"],
		function(self)
			local CharacterName = CharacterInputBox:GetText()
			local FolderName = FolderInputBox:GetText():lower()
			if CharacterName == "" then
				print(L["SOUNDTAQ_NAMEERROR"])
			elseif CharacterName == "暗夜小瞳" or CharacterName == "Babyannie" then
				print(L["SOUNDTAQ_DEFAULTERROR"])
			else
				if Addon.CharacterList[CharacterName] then
					if FolderName == "delete" then
						if SoundList.CurrentValue == Addon.CharacterList[CharacterName] then
							SoundList.CurrentValue = "xiaotong"
						end
						Addon.CharacterList[CharacterName] = nil
						print(string.format(L["SOUNDTAQ_DELETE"], CharacterName))
					elseif FolderName == "" then
						print(L["SOUNDTAQ_FOLDERERROR"])
					else
						Addon.CharacterList[CharacterName] = FolderName
						print(string.format(L["SOUNDTAQ_MODIFY"], CharacterName, FolderName))
					end
				else
					if FolderName == "" then
						print(L["SOUNDTAQ_FOLDERERROR"])
					else
						Addon.CharacterList[CharacterName] = FolderName
						print(string.format(L["SOUNDTAQ_ADD"], CharacterName, FolderName))
					end
				end
			end
			ScrollFrame:ConfigOkay()
		end)
	AddSound:SetPoint("LEFT", FolderInputBox, "RIGHT", 20, 0)
end

--面板初始化
Panel:Show()
ScrollFrame.okay = ScrollFrame.ConfigOkay
ScrollFrame.default = ScrollFrame.ConfigDefault
ScrollFrame.refresh = ScrollFrame.ConfigRefresh
ScrollFrame:ConfigRefresh()
ScrollFrame:Show()